package com.crm.services;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.crm.model.jdao.IAuthorityDAO;
import com.crm.model.jdao.ISalesTrackingLogDAO;
import com.crm.model.pojo.Power;
import com.crm.model.pojo.SalesOpportunity;
import com.crm.model.pojo.SalesOpportunityTrackingLog;

@Service
public class SaleTrackingLogServices {

	@Autowired
	private ISalesTrackingLogDAO salesTrackingLogDAO;
	@Autowired
	private DictionaryServices ds;
	@Autowired
	private UserServices us;	
	
	/**
	 * 查询全部某个销售机会的销售机会跟踪日志
	 * @return 
	 */
	public JSONArray queryBySalId(int salId) {
		JSONArray array =new JSONArray();

		List<SalesOpportunityTrackingLog> list=salesTrackingLogDAO.queryBySalId(salId);
		for (SalesOpportunityTrackingLog salesOpportunity : list) {
		//System.out.println("salesOpportunity:"+salesOpportunity);
			JSONObject obj = new JSONObject();		
			array.add(toJSONObject1( obj,salesOpportunity));
	}
		return array;
	}
	
	/**
	 * 销售机会转日志JSONObject
	 * @param obj
	 * @param salesOpportunity
	 * @return
	 */
	public JSONObject toJSONObject1(JSONObject obj,SalesOpportunityTrackingLog salesOpportunity) {
	//	obj.put("salId", salesOpportunity.getSotlId());
		obj.put("UName", us.findByUId(salesOpportunity.getSotlName()).getUName()+"");
		obj.put("sotlPossibility", ds.findByDictId(salesOpportunity.getSotlPossibility()));
		obj.put("sotlMoney", salesOpportunity.getSotlMoney()+"");
		obj.put("SotlStage", ds.findByDictId(salesOpportunity.getSotlStage()));
		obj.put("updateTime", salesOpportunity.getUpdateTime().toString());
		obj.put("notes", salesOpportunity.getNotes()+"");
		if(salesOpportunity.getSotlDate()!=null) {
			obj.put("sotlDate",salesOpportunity.getSotlDate().toString());			
		}				
		//obj.put("", value);
		return obj;
	}
	
	
}
